var pageSize = 10;
var pageNum = 1;
var page;
var laypage;



$(function () {
    $("#menuId").val(sessionStorage.menuId);
    var menuId = $("#menuId").val();
    if(sessionStorage.getItem(menuId+'add') == 1){
        $("#addBanner").css('display','inline');
    }
    // alert("000"+sessionStorage.menuId);
    layui.use(['laydate','element','laypage','layer'], function(){
        $ = layui.jquery;//jquery
        laydate = layui.laydate;//日期插件
        element = layui.element();//面包导航
        laypage = layui.laypage;//分页
        layer = layui.layer;//弹出层

        //以上模块根据需要引入

        var start = {
            // min: laydate.now(),
            max: '2099-06-16 23:59:59'
            ,istoday: true
            ,choose: function(datas){
                end.min = datas; //开始日选好后，重置结束日的最小日期
                end.start = datas //将结束日的初始值设定为开始日
            }
        };

        var end = {
            // min: laydate.now(),
            max: '2099-06-16 23:59:59'
            ,istoday: false
            ,choose: function(datas){
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };

        document.getElementById('LAY_demorange_s').onclick = function(){
            start.elem = this;
            laydate(start);
        }
        document.getElementById('LAY_demorange_e').onclick = function(){
            end.elem = this
            laydate(end);
        }
    });
    setTimeout(function () {
        findWithdrawalList();
    },200);

});



//查询提现审核列表
function findWithdrawalList(){
    var menuId = $("#menuId").val();
    var startTime = $("#LAY_demorange_s").val();
    var endTime = $("#LAY_demorange_e").val();
    var isAuditPass = $("#isAuditPass").val();
    // var cashFlowType = $("#cashFlowType").val();
    var state = $("#state").val();
    var phone = $("#phone").val();
    if (phone != "") {
        if (!(/^1[3|4|5|7|8]\d{9}$/.test(phone))) {
            alert("请输入有效的手机号码！");
            return false;
        }
    }

    var params = {
        "type":"query",
        "menuId":menuId,
        "pageSize":pageSize,
        "pageNum":pageNum,
        "startTime":startTime,
        "endTime":endTime,
        "isAuditPass":isAuditPass,
        "cashFlowType":4,
        "state":state,
        "phone":phone,
        "orderType":4
    };
    $.ajax({
        type:"post",
        url: "/order/findWithdrawalList",
        data:params,
        dataType: "json",
        success : function(data){
            if(data.code == 1 && data.code != undefined){
                if(data.withdrawalHistoryOrders == null || data.withdrawalHistoryOrders.length <= 0){
                    $("#bus").html('<tr><td colspan="16" style="text-align: center">暂无数据</td></tr>');
                    $("#total").html("共有数据：0 条");
                    return;
                }
                var list = data.withdrawalHistoryOrders;
                page = data.page;
                var html = "";
                for(var b in list){

                    var state = "";
                    if(list[b].state == 1 || list[b].state == 4){
                        state = "提交";
                    }else if(list[b].state == 2){
                        state = "完成";
                    }else if(list[b].state == 3){
                        state = "失败";
                    }

                    var type = "";
                    if (list[b].type == 1) {
                        type = "零钱提现";
                    }else if (list[b].type == 2) {
                        type = "推荐奖励金提现";
                    }else if (list[b].type == 3){
                        type = "学习奖励金提现";
                    }else if(list[b].type == 4){
                        type = "消费金营业额提现";
                    }

                    html += '<tr><td>'+list[b].userId+'</td>' +
                        '<td>'+list[b].id+'</td>' +
                        '<td>'+(list[b].nickName == null ? "" : list[b].nickName)+'</td>' +
                        '<td>'+(list[b].phone == null ? "" : list[b].phone)+'</td>' +
                        '<td>'+(list[b].createTime == null ? "" : new Date(list[b].createTime).toLocaleString())+'</td>' +
                        '<td>'+(list[b].finishTime == null ? "" : new Date(list[b].finishTime).toLocaleString()) +'</td>' +
                        '<td>'+(list[b].modifyTime == null ? "" : new Date(list[b].modifyTime).toLocaleString())+'</td>' +
                        '<td>'+list[b].price+'</td>'+
                        '<td>'+list[b].handlingCharge+'</td>'+
                        // '<td style="word-wrap:break-word;word-break:break-all;width: 150px">'+list[b].account+'</td>'+
                        '<td style="word-wrap:break-word;word-break:break-all;">'+list[b].thirdPartyPayNo+'</td>'+
                        '<td>'+state+'</td>'+
                        '<td>'+type+'</td>'+
                        '<td>'+list[b].accountName+'</td>'+
                        '<td>'+list[b].currentApproverName+'</td><td>';

                    if(sessionStorage.getItem(menuId+'edit') == 1) {
                        if (list[b].isAuditPass == 0 && (list[b].state == 1 || list[b].state == 3)) {
                            html += '<button onclick="showAuditPage(' + list[b].id + ')" value="1" class="layui-btn layui-btn-normal">审核</button>';
                        }
                    }
                    html += '</td></tr>';
                }
                $("#bus").html(html);
                $("#total").html("共有数据："+page.total+" 条");
                laypage({
                    cont: 'page'
                    ,pages: page.pages
                    ,first: 1
                    ,curr:pageNum
                    ,last: page.pages
                    ,prev: '<em><</em>'
                    ,next: '<em>></em>'
                    ,jump: function (obj,first) {
                        pageNum = obj.curr;
                        if(!first){
                            findWithdrawalList();
                        }
                    }
                });
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
            }else if(data.code == 2 && data.code != undefined){
                alert(data.message);
            }
        }

    });
}

//查询按钮
function search() {
    pageNum = 1;
    findWithdrawalList();
}

//打开审核页面
var index;
function showAuditPage(id) {
    var html = "<textarea id='content' style='width: 497px;height: 180px;resize: none'></textarea><button id='bt1' onclick='through("+id+","+1+",this)' class='layui-btn layui-btn-normal' style='margin-left: 100px !important;'>通过</button><button id='bt2' onclick='through("+id+","+2+",this)' class='layui-btn layui-btn-danger' style='margin-left: 100px !important;'>不通过</button>"
    index = layer.open({
        title: ['提现审核','color: #2299ee;border-bottom: 1px solid;']
        ,type: 1
        // ,btn:['通过','不通过']
        // ,btn1:function () {
        //     through(id,1,$("#content").val());
        // }
        // ,btn2:function () {
        //     through(id,2,$("#content").val());
        // }
        ,content: html
        ,area: ['500px', '300px']
        ,maxmin:true
    });
}

//审核
function through(id,auditType,obj) {
    $("#bt1").attr("disabled",true);
    $("#bt1").attr("class","layui-btn layui-btn-disabled");
    $("#bt2").attr("disabled",true);
    $("#bt2").attr("class","layui-btn layui-btn-disabled");
    var content =$("#content").val();
    if(auditType == 2){
        if(content == "" || content == undefined){
            alert("请填写审核信息");
            $(obj).attr("disabled",false);
            $(obj).attr("class","layui-btn layui-btn-danger");
            $("#bt1").attr("disabled",true);
            $("#bt1").attr("class","layui-btn layui-btn-normal");
            return false;
        }
    }
    var menuId = $("#menuId").val();
    var params = {
        "type":"change",
        "menuId":menuId,
        "id":id,
        "auditType":auditType,
        "content":content
    };
    $.ajax({
        type:"post",
        url:"/order/withdrawaAudit",
        data:params,
        dataType:"json" ,
        success :function (data) {

            if(data.code == 1 && data.code != undefined){
                alert(data.message);
                layer.close(index);
                findWithdrawalList();
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
                layer.close(index);
            }else if(data.code == 2 && data.code != undefined){
                alert(data.message);
                layer.close(index);
                findWithdrawalList();
            }else if(data.code == 3 && data.code != undefined){
                alert("系统异常,请联系管理员");
                layer.close(index);
                findWithdrawalList();
            }
        }
    });
}
